﻿using Core.DataAccess.Model.iTool.Gateway;
using HandlerContextOptions;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
using UserCenter.Interfaces;

namespace UserCenter.Implements
{
    public class RoleMenu : IRoleMenu
    {
        public async Task<RoleMenuDeleteResult> Delete(RoleMenuDeleteRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var menu = await context.SysRoleMenus.FindAsync(requset.MenuKey);
                if (menu == null)
                {
                    return new RoleMenuDeleteResult
                    {
                        IsSuccessful = false,
                        Description = "菜单不存在"
                    };
                }
                else
                {
                    context.SysRoleMenus.Remove(menu);
                    await context.SaveChangesAsync();

                    return new RoleMenuDeleteResult
                    {
                        IsSuccessful = true
                    };
                }
            }
        }
        public async Task<GetRoleMenuResult> Get(GetRoleMenuRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                if (requset.RoleId > 0)
                {

                    return new GetRoleMenuResult
                    {
                        RoleMenus = await context.SysRoleMenus.Where(item => item.RoleMenuRoleId == requset.RoleId).ToListAsync(),
                        IsSuccessful = true
                    };

                }
                else
                {
                    return new GetRoleMenuResult
                    {

                        RoleMenus = await context.SysRoleMenus.ToListAsync(),
                        IsSuccessful = true
                    };
                }
            }
        }
        public async Task<RoleMenuInsertResult> Insert(RoleMenuInsertRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                await context.SysRoleMenus.AddAsync(requset);
                await context.SaveChangesAsync();

                return new RoleMenuInsertResult { IsSuccessful = true };
            }
        }
        public async Task<RoleMenuModifyResult> Modify(RoleMenuModifyRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var menu = await context.SysRoleMenus.FindAsync(requset.RoleMenuId);
                if (menu == null)
                {
                    return new RoleMenuModifyResult
                    {
                        IsSuccessful = false,
                        Description = "菜单不存在"
                    };
                }
                else
                {
                    context.SysRoleMenus.Update(menu);

                    await context.SaveChangesAsync();

                    return new RoleMenuModifyResult
                    {
                        IsSuccessful = true
                    };
                }
            }
        }
    }
}
